import { useEffect, useState } from 'react'
import Taro from '@tarojs/taro'

interface NavBarInfo {
  statusBarHeight: number
  navBarHeight: number
  totalHeight: number
  statusBarHeightRpx: number
  navBarHeightRpx: number
  totalHeightRpx: number
  menuButton: Taro.getMenuButtonBoundingClientRect.Rect
}

const useNavBarInfo = (): NavBarInfo => {
  const [info, setInfo] = useState<NavBarInfo>({
    statusBarHeight: 20,
    navBarHeight: 44,
    totalHeight: 64,
    statusBarHeightRpx: 40,
    navBarHeightRpx: 88,
    totalHeightRpx: 128,
    menuButton: {
      width: 0,
      height: 0,
      top: 0,
      bottom: 0,
      left: 0,
      right: 0,
    }
  })

  useEffect(() => {
    try {
      const sysInfo = Taro.getSystemInfoSync()
      const menuButton = Taro.getMenuButtonBoundingClientRect()
      const statusBarHeight = sysInfo.statusBarHeight || 20
      const navBarHeight = menuButton.height + (menuButton.top - statusBarHeight) * 2
      const totalHeight = statusBarHeight + navBarHeight

      setInfo({
        statusBarHeight,
        navBarHeight,
        totalHeight,
        statusBarHeightRpx: statusBarHeight * 2,
        navBarHeightRpx: navBarHeight * 2,
        totalHeightRpx: totalHeight * 2,
        menuButton
      })
    } catch (e) {
      console.warn('获取导航栏信息失败:', e)
    }
  }, [])

  return info
}

export default useNavBarInfo
